<?php

namespace App\Http\Middleware;

use App\Webdisk\Model\User;
use Closure;

use function App\Webdisk\makeRes;

class CheckLogin
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $token = $request->header('utoken', $request->input('utoken', $request->cookie('utoken')));
        if( $token == null){
            return response(makeRes(-1,null,"未登录，请先登录"),403);
        }else{
            $data = User::getByToken($_COOKIE['utoken']);
            if(!$data){
                return response(makeRes(-1,null,"登录无效或已过期，请重新登录"),403);
            }else{
                $request->attributes->set("user",$data);
            }
        }
        return $next($request);
    }
}
